//
//  XMStatus.swift
//  XMWeibo-Swift
//
//  Created by 育鑫 王 on 15/11/1.
//  Copyright © 2015年 育鑫 王. All rights reserved.
//

import UIKit

class XMStatusCell: UITableViewCell {
    
    // MARK: - 属性
    var pictureViewWidthCons : NSLayoutConstraint?
    
    var pictureViewHeightCons : NSLayoutConstraint?
    
    /// 微博模型
    var status: XMStatus? {
        didSet {
            // 将模型赋值给topView
            topView.status = status
            
            // 将模型赋值给配图视图
            pictureView.status = status
            
            let size = pictureView.calcViewSize()
            // 重新设置配图的宽高约束
            pictureViewWidthCons?.constant = size.width
            pictureViewHeightCons?.constant = size.height
            
            // 设置微博内容
            contentLabel.text = status?.text
        }
    }
    
    // 设置cell的模型,cell会根据模型,重新设置内容,更新约束,获取子控件的最大Y值
    // 返回cell的高度
    func rowHeight(status :XMStatus) -> CGFloat {
        // 设置cell的模型
        self.status = status
        
        // 更新约束
        layoutIfNeeded()
        
        // 获取子控件的最大值
        let maxY = CGRectGetMaxY(bottomView.frame)
        
        return maxY
    }
    
    // MARK: - 构造函数
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        prepareUI()
    }

    // MARK: - 准备UI
    func prepareUI () {
        // 添加子控件
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        
        
        // 添加约束
        topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 53))
        
        // 微博内容
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: 8, y: 8))
        
        // 设置宽度
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: UIScreen.mainScreen().bounds.width - 2 * 8))
        
//        // 图片视图
//        let cons = pictureView.ff_AlignInner(type: ff_AlignType.BottomLeft, referView: contentLabel, size: CGSize(width: 0, height: 0), offset: CGPoint.init(x: 0, y: 8))
//        // 获取配图视图的宽度约束
//        pictureViewWidthCons = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Width)
//        // 获取配图视图的高度约束
//        pictureViewHeightCons = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Height)
        
//        // 图片视图底部与底部视图顶部对齐
//        addConstraint(NSLayoutConstraint(item: pictureView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: bottomView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        // 底部视图
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 44), offset: CGPoint(x: -8, y: 8))
        
        //        // 添加contentView底部和bottomView的底部重合
        //        contentView.addConstraint(NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: bottomView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    }
    
    // MARK - 懒加载
    private lazy var topView: XMStatusTopView = XMStatusTopView()
    
    
    // 微博内容
    lazy var contentLabel: UILabel = {
        let label = UILabel(fonsize: 16, textColor: UIColor.blackColor())
        
        // 显示多行
        label.numberOfLines = 0
        
        return label
    }()
    
    /// 底部视图
    lazy var bottomView: CZStatusBottomView = CZStatusBottomView()
    
    /// 图片视图
    lazy var pictureView: XMStatusPictureView = XMStatusPictureView()
    
}
